package 力扣日常刷题.木22二月.第11天217;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author 帅小伙
 * @date 2022/2/17
 * @description
 */
public class Demo82无重复字符的最长字串 {

    public int lengthOfLongestSubstring(String s) {
        HashSet<Character> set = new HashSet<>();
        int r = -1, ans = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            if (i != 0) set.remove(s.charAt(i - 1));
            while (r + 1 < n && !set.contains(s.charAt(r + 1))) {
                set.add(s.charAt(r + 1));
                ++r;
            }
            ans = Math.max(ans, r - i + 1);
        }
        return ans;
    }

}
